using System;
using System.Collections.Generic;

namespace MiniFlower
{
    public class SubsystemCollection<TSubsystem> where TSubsystem : ISubsystem
    {
        private Dictionary<Type, ISubsystem> m_SubsystemMap = new Dictionary<Type, ISubsystem>();

        public SubsystemCollection()
        {
            m_SubsystemMap.Clear();
        }

        public void Initialize()
        {

        }

        public void Deinitialize()
        {

        }

        public void RegisterSubsystem(TSubsystem subsystem)
        {


            Type type = subsystem.GetType();
            m_SubsystemMap.TryAdd(type, subsystem);
        }

        public void UnregisterSubsystem(TSubsystem subsystem)
        {
            Type type = subsystem.GetType();
            m_SubsystemMap.Remove(type);
        }

        public TConcreteSubsystem GetSubsystem<TConcreteSubsystem>() where TConcreteSubsystem : ISubsystem
        {
            Type subsystemType = typeof(TConcreteSubsystem);
            if (m_SubsystemMap.TryGetValue(subsystemType, out ISubsystem value))
            {
                return (TConcreteSubsystem)value;
            }

            return default;
        }
    }
}
